package com.crazymaker.cloud.seata.seckill.service.impl;

import com.crazymaker.cloud.seata.seckill.feign.SeataDemoOrderFeignClient;
import com.crazymaker.cloud.seata.seckill.feign.SeataDemoStockFeignClient;
import com.crazymaker.springcloud.seckill.api.dto.SeckillDTO;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 基于Seata AT模式实现的分布式事务解决方案
 */
@Configuration
@Slf4j
@Service
public class SeataSeckillServiceImpl {
    @Autowired
    private SeataDemoOrderFeignClient stockFeignClient;
    @Autowired
    private SeataDemoStockFeignClient orderFeignClient;

    /**
     * TM事务管理器：全局事务的发起方
     *  发起方会向TC事务协调者申请一个全局事务id，并保存到ThreadLocal中
     *
     * 减库存，下订单
     */
    @GlobalTransactional
    public void doSeckill(@RequestBody SeckillDTO dto) {
        orderFeignClient.minusStock(dto);
        stockFeignClient.addOrder(dto);
    }
}
